package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.constant.RentalCacheConstant;
import org.jeecg.modules.rental.entity.RentalCouponCategory;
import org.jeecg.modules.rental.entity.RentalStore;
import org.jeecg.modules.rental.entity.RentalStoreCategory;
import org.jeecg.modules.rental.service.IRentalStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Description: 租车网点
* @Author: sean
* @Date:   2022-03-29
* @Version: V1.0
*/
@Api(tags="租车网点")
@RestController
@RequestMapping("/rental/store")
@Slf4j
public class RentalStoreController extends JeecgController<RentalStore, IRentalStoreService> {


    @Autowired
    private IRentalStoreService rentalStoreService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
//
//    @Autowired
//    private CacheManager cacheManager;

   /**
    * 分页列表查询
    * @param req
    * @param pageNo
    * @param pageSize
    * @return
    */
   @ApiOperation(value="分页列表查询", notes="分页列表查询")
   @GetMapping(value = "/list")
   @PermissionData(pageComponent="rental/store/list")
   public Result<Page<RentalStore>>queryPageList(RentalStore req,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 HttpServletRequest request) {
       QueryWrapper<RentalStore> queryWrapper = QueryGenerator.initQueryWrapper(req, request.getParameterMap());
       Page<RentalStore> page = new Page<RentalStore>(pageNo, pageSize);
       return Result.OK(rentalStoreService.page(page, queryWrapper));
   }

    @ApiOperation(value="列表查询", notes="列表查询")
    @GetMapping(value = "/list-no-page")
    @PermissionData(pageComponent="rental/store/list")
    public Result<List<RentalStore>>queryPageList(RentalStore req, HttpServletRequest request) {
        QueryWrapper<RentalStore> queryWrapper = QueryGenerator.initQueryWrapper(req, request.getParameterMap());
        return Result.OK(rentalStoreService.list(queryWrapper));
    }

    /**
     * 通过经纬度查询最近的门店
     * @param latitude
     * @param longitude
     * @param request
     * @return
     */
    @ApiOperation(value="门店网点-通过位置查询", notes="门店网点-通过位置查询")
    @GetMapping(value = "/shops-by-location")
    public Result<List<RentalStore>> shopsByLocation(@RequestParam(name="latitude",required=true) Double latitude,
                                                        @RequestParam(name="longitude",required=true) Double longitude,
                                                        HttpServletRequest request) {
        QueryWrapper<RentalStore> queryWrapper = QueryGenerator.initQueryWrapper(new RentalStore(), request.getParameterMap());
        List<RentalStore> shopList = rentalStoreService
                .shopListByLocation(latitude, longitude, queryWrapper);
        return Result.OK(shopList);
    }

   /**
    *   添加
    *
    * @param req
    * @return
    */
   @AutoLog(value = "网点-添加")
   @ApiOperation(value="网点-添加", notes="网点-添加")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody RentalStore req) {
       rentalStoreService.add(req);
       return Result.OK("common.add.success");
   }


//	 /**
//	  * 网点的网格管理员列表
//	  * @return
//	  */
//	 @ApiOperation(value="网点的网格管理员列表", notes="网点的网格管理员列表")
//	 @PostMapping(value = "/unbound")
//	 public Result<List<UnboundMeshPointPersonnelRsp>> gentUnbound() {
//		 List<UnboundMeshPointPersonnelRsp> var =  rentalStoreService.gentUnbound();
//		 return Result.OK(var);
//	 }


//	 /**
//	  * 网点信息
//	  * @return
//	  */
//	 @ApiOperation(value="网点信息", notes="网点信息")
//	 @GetMapping(value = "/info")
//	 public Result<ChargeMeshInfoRsp> getChargeMeshInfoInfo(@ApiParam(value = "网点id", name = "id")
//																@RequestParam(value = "id", required = true) String id) {
//		 ChargeMeshInfoRsp var =  rentalStoreService.getChargeMeshInfoInfo(id);
//		 return Result.OK( var);
//	 }


   /**
    *  编辑
    *
    * @param req
    * @return
    */
   @AutoLog(value = "编辑")
   @ApiOperation(value="编辑", notes="编辑")
   @PostMapping(value = "/edit")
   public Result<String> edit(@RequestBody @Valid RentalStore req) {
       rentalStoreService.edit(req);
       return Result.OK("common.edit.success");
   }


    /**
     * 管理员修改状态
     * @return
     */
    @ApiOperation(value="管理员修改状态", notes="管理员修改状态")
    @AutoLog(value = "修改网点状态")
    @GetMapping(value = "/modify_state")
    public Result modifyState(@ApiParam(value = "网点id", name = "id")
                                   @RequestParam(value = "id", required = true) String id,
                              @ApiParam(value = "网点状态：1启用，2维护", name = "meshPointsType")
                              @RequestParam(value = "meshPointsType", required = true) Integer meshPointsType) {
        rentalStoreService.modifyState(id,meshPointsType);
        return Result.OK("");
    }


    /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "门店网点-通过id删除")
   @ApiOperation(value="门店网点-通过id删除", notes="门店网点-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<String> delete(@RequestParam(name="id",required=true) String id) {
       redisTemplate.opsForGeo().remove(RentalCacheConstant.RENTAL_STORE_LOCATION_CACHE, id);
       rentalStoreService.removeById(id);
       return Result.OK("common.delete.success");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "门店网点-批量删除")
   @ApiOperation(value="门店网点-批量删除", notes="门店网点-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       String[] idStr = ids.split(",");
       redisTemplate.opsForGeo().remove(RentalCacheConstant.RENTAL_STORE_LOCATION_CACHE, idStr);
       rentalStoreService.removeByIds(Arrays.asList(idStr));
       return Result.OK("common.delete.batch.success");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @ApiOperation(value="门店网点-通过id查询", notes="门店网点-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<RentalStore> queryById(@RequestParam(name="id",required=true) String id) {
       RentalStore chargeMeshPoint = rentalStoreService.getById(id);
       if(chargeMeshPoint==null) {
           return Result.error("common.query.not.found",null);
       }
       return Result.OK(chargeMeshPoint);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param chargeMeshPoint
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, RentalStore chargeMeshPoint) {
       return super.exportXls(request, chargeMeshPoint, RentalStore.class, "门店网点");
   }

//    /**
//     *通过经纬度查询最近的门店
//     * @param latitude 维度
//     * @param longitude 经度
//     * @return
//     */
//    @ApiOperation(value="门店网点-通过位置查询", notes="门店网点-通过位置查询")
//    @GetMapping(value = "/shop-by-location")
//    public Result<ShopPointLocationDto> shopByLocation(
//            @RequestParam(name="latitude",required=true) Double latitude,
//            @RequestParam(name="longitude",required=true) Double longitude) {
//        ShopPointLocationDto chargeMeshPoint = rentalStoreService.shopByLocation(latitude, longitude, OrgThreadHolder.get());
//        return Result.OK(chargeMeshPoint);
//    }





//    @ApiOperation(value="租车区域内的网点-列表查询", notes="租车区域内的网点-列表查询")
//    @GetMapping(value = "/region-stores")
//    public Result<List<RentalStore>> orgRegions(@RequestParam(name="sysOrgCode",required=true) String sysOrgCode) {
//        List<RentalStore> list = rentalStoreService.list( new QueryWrapper<RentalStore>()
//                .eq( "sys_org_code", sysOrgCode));
//        return Result.OK(list);
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="门店网点-通过id查询", notes="门店网点-通过id查询")
    @GetMapping(value = "/detail")
    public Result<RentalStore> detail(@RequestParam(name="id",required=true) String id) {
        RentalStore chargeMeshPoint = rentalStoreService.getById(id);
        if(chargeMeshPoint==null) {
            return Result.error("common.query.not.found",null);
        }
        return Result.OK(chargeMeshPoint);
    }

    @ApiOperation("商户网点列表")
    @GetMapping("/store/list")
    public Result<List<Map<String, Object>>> getStoreList() {
        try {
            List<Map<String, Object>> result = rentalStoreService.getAllStores();
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取商户网点列表失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

}
